//
//  ST_Cupid'sArrowView.swift
//  btvideo
//
//  Created by mac on 2020/7/8.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyJSON

class ST_CupidsArrowView: UIView {
    
    static let width = ScreenWidth
    
    static let height = ScreenWidth/375.0*270
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(back_image_view)
        back_image_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.addSubview(close_btn)
        close_btn.snp.makeConstraints { (make) in
            make.width.height.equalTo(44)
            make.top.equalToSuperview().offset(23-(44-18)/2.0)
            make.right.equalToSuperview().offset(-25+(44-18)/2.0)
        }
        
        
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(28/375.0*ScreenWidth)
            make.height.equalTo(22/375.0*ScreenWidth)
        }
        
        self.addSubview(avatar_image_view)
        avatar_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(avatar_image_view_height)
            make.top.equalTo(titleLabel.snp.bottom).offset(20/375.0*ScreenWidth)
            make.centerX.equalToSuperview()
        }
        
        self.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(avatar_image_view.snp.bottom).offset(10/375.0*ScreenWidth)
            make.height.equalTo(20/375.0*ScreenWidth)
        }
        
        self.addSubview(chat_btn)
        chat_btn.snp.makeConstraints { (make) in
            make.height.equalTo(chat_btn_height)
            make.left.equalToSuperview().offset(58)
            make.right.equalToSuperview().offset(-57)
            make.bottom.equalToSuperview().offset(-32/375.0*ScreenWidth)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //    MARK:- Bind Data
    var json = JSON.null{
        didSet{
            avatar_image_view.pin_setImage(from: json["avatar"].stringValue.compress_small_image(width: 74*3).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            content_label.text = json["info_link"].stringValue
            
         
            BINP.update_base_info(remoteInfo: ST_BTUserInfoFromRemote.init(accid: json["accid"].stringValue, avatar: json["avatar"].stringValue, userName: json["nickname"].stringValue))
        }
    }
    //    MARK:- Properties
    
    
    fileprivate lazy var back_image_view:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("Copid_sArrowBack"))
        return control
    }()
    
    fileprivate lazy var close_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImageForAllStates(ST_ImageUtils.imageName("注册_icon_关闭")!)
        control.addTarget(self, action: #selector(close_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        var ta = "TA"
        if ST_BTUserInfoManager.shared.current_account_info?.userinfo?.sex == .male{
            ta = "她"
        }else{
            ta = "他"
        }
        control.text = "你的丘比特之箭射中了\(ta)哟！"
        control.textColor = .white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 16/375.0*ScreenWidth, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = .white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14/375.0*ScreenWidth, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var chat_btn_height = 44/375.0*ScreenWidth
    
    fileprivate lazy var chat_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = UIColor.white
        control.cornerRadius = chat_btn_height/2.0
        control.setTitleForAllStates("立即聊天")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 16/375.0*ScreenWidth, weight: UIFont.Weight.semibold)
        control.setTitleColorForAllStates(UIColor.init(hex: 0xFEC207)!)
        control.addTarget(self, action: #selector(chat_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var avatar_image_view_height = 74/375.0*ScreenWidth
    
    fileprivate lazy var avatar_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = .scaleAspectFill
        control.cornerRadius = avatar_image_view_height/2.0
        control.borderColor = UIColor.white
        control.borderWidth = 1
        
        return control
    }()
}


//MARK: - Event
extension ST_CupidsArrowView{
    
    @objc fileprivate func chat_handle(){
        
        let vc = ST_PrivateChatViewController()
        
        vc.sessionID = json["accid"].stringValue
        
        vc.other_chat_source = 3
        
        vc.source = "丘比特匹配"
            
        GlobalPresent(toController: vc)
        
        self.isHidden = true
    }
    
    @objc fileprivate func close_handle(){
        
        self.isHidden = true
        
        JVrequest(PH.unlikePersonApi,parameters:["target_accid":json["accid"].stringValue],success: { (json) in
                
//            print("碰一碰不喜欢成功")
        }) { (code, err) in
            
//            print("碰一碰不喜欢失败:",err)
        }
        
        
    }
}
